package com.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Created by Administrator on 2017/6/11.
 */
public class TimeClient {

    public  void connect(int port,String host) throws  Exception {
        //处理读写业务的线程池组
        EventLoopGroup clientGroup=new NioEventLoopGroup();
        try {

        //bootstrap辅助启动类
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.group(clientGroup)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY,true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline().addLast(new TimeClientHandler());
                    }
                });

            ChannelFuture channelFuture=bootstrap.connect(host,port).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            clientGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        try {
            new TimeClient().connect(8088,"127.0.0.1");
            System.out.println("客户端连接");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
